from typing import List, Optional
from sqlalchemy.orm import Session

from app.db.models import Job  # 假设你的 Job 模型在这里
from app.schemas.job import JobCreate, JobUpdate # 假设你的 Pydantic schema 在这里
from .base import CRUDBase # 假设你有通用的 CRUDBase

class CRUDJob(CRUDBase[Job, JobCreate, JobUpdate]):
    def get_by_spider_id(self, db: Session, *, spider_id: str) -> List[Job]:
        return db.query(Job).filter(Job.spider_id == spider_id).all()
    
    def get_active_jobs(self, db: Session, *, spider_id: str) -> List[Job]:
        return db.query(Job).filter(
            Job.spider_id == spider_id,
            Job.status.in_([JobStatus.QUEUED, JobStatus.STARTING, JobStatus.RUNNING]),
            Job.job_type == JobType.SPIDER_RUN
        ).all()
    
    def get_active_dependency_jobs(self, db: Session, *, spider_id: str) -> List[Job]:
        return db.query(Job).filter(
            Job.spider_id == spider_id,
            Job.status.in_([JobStatus.QUEUED, JobStatus.STARTING, JobStatus.RUNNING]),
            Job.job_type == JobType.DEPENDENCY_INSTALLATION
        ).all()

crud_job = CRUDJob(Job)